﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using 尝试封装.DataSet1TableAdapters;

namespace 尝试封装
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlHelper.ExecuteNonQuery("insert into Person_1(Name,Age) values(@name,@age)",
                new SqlParameter("name", "勾搭女"), new SqlParameter("age", 16));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            object obj = SqlHelper.ExecuteScalar("select * from Person_1");
            MessageBox.Show(Convert.ToString(obj));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from Person_1");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                string name = Convert.ToString(row["Name"]);
                MessageBox.Show(name);
            }
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Users封装 where Username = @username",
                new SqlParameter("username", txtUsername.Text));
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("账号不存在！");
                return;
            }
            else
            {
                DataRow row = dt.Rows[0];
                int errorTimes = Convert.ToInt32(row["ErrorTimes"]);
                if (errorTimes >= 3)
                {
                    MessageBox.Show("登陆过于频繁，请稍后再登！");
                    return;
                }
                string pw = Convert.ToString(row["Password"]);
                if (txtPassword.Text == pw)
                {
                    MessageBox.Show("登陆成功！");
                    SqlHelper.ExecuteNonQuery("update T_Users封装 set ErrorTimes = 0 where Username=@username",
                        new SqlParameter("username", txtUsername.Text));
                }
                else
                {
                    MessageBox.Show("密码错误！");
                    SqlHelper.ExecuteNonQuery("update T_Users封装 set ErrorTimes = ErrorTimes+1 where Username=@username",
                        new SqlParameter("username", txtUsername.Text));
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataSet dataset = new DataSet();
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_Users";
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dataset);
                    DataTable dt = dataset.Tables[0];
                    DataRow row = dt.Rows[0];
                    row["Username"] = "Linda";

                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                    
                    adapter.Update(dataset);
                }
            }
        }


        private void button5_Click(object sender, EventArgs e)
        {
            T_UsersTableAdapter adapter = new T_UsersTableAdapter();
            尝试封装.DataSet1.T_UsersDataTable data = adapter.GetData();
            for (int i = 0; i < data.Count; i++)
            {
                尝试封装.DataSet1.T_UsersRow row = data[i];
                MessageBox.Show(row.Username);
            }
        }
    }
}
